package com.geek.pokermanager.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.geek.pokermanager.model.PokerFaceFile;

public class FileTools {

	@SuppressWarnings({ "unused", "unchecked" })
	public static Map getFile(String path) throws IOException {
		File file = new File(path);
		File[] array = file.listFiles();
		Map<String, List<PokerFaceFile>> orderMap = new HashMap<String, List<PokerFaceFile>>();
		for (int i = 0; i < array.length; i++) {
			PokerFaceFile poker = new PokerFaceFile();
			if (array[i].isFile()) {
				String fileAllName = array[i].getName();
				String fileName = fileAllName.substring(0, fileAllName.indexOf("."));
				String filePresuffix = fileName.trim().substring(0, 1);
				poker.setPokerFileName(fileAllName);
				poker.setPokerFileRealName(fileName);
				poker.setPrefix(filePresuffix);
				poker.setIndexVal(fileName.substring(1).length()<3?Integer.valueOf(fileName.substring(1)):0);
				poker.setPath("image/pokerFace/"+fileAllName);
				switch (filePresuffix) {
				case "f":
					if (orderMap.get("fangkuai") == null) {
						List<PokerFaceFile> fangkuai = new ArrayList<PokerFaceFile>();
						orderMap.put("fangkuai", fangkuai);
					}
					orderMap.get("fangkuai").add(poker);
					break;
				case "h":
					if (orderMap.get("hongxin") == null) {
						List<PokerFaceFile> hongxin = new ArrayList<PokerFaceFile>();
						orderMap.put("hongxin", hongxin);
					}
					orderMap.get("hongxin").add(poker);
					break;
				case "m":
					if (orderMap.get("meihua") == null) {
						List<PokerFaceFile> meihua = new ArrayList<PokerFaceFile>();
						orderMap.put("meihua", meihua);
					}
					orderMap.get("meihua").add(poker);
					break;
				case "t":
					if (orderMap.get("tao") == null) {
						List<PokerFaceFile> tao = new ArrayList<PokerFaceFile>();
						orderMap.put("tao", tao);
					}
					orderMap.get("tao").add(poker);
					break;
				default:
					if (orderMap.get("default") == null) {
						List<PokerFaceFile> defaultPoker = new ArrayList<PokerFaceFile>();
						orderMap.put("default", defaultPoker);
					}
					orderMap.get("default").add(poker);
					break;
				}
			}
		}
		Collections.sort(orderMap.get("tao"), new orderVal());
		Collections.sort(orderMap.get("meihua"), new orderVal());
		Collections.sort(orderMap.get("hongxin"), new orderVal());
		Collections.sort(orderMap.get("fangkuai"), new orderVal());
		return orderMap;
	}
	
	@SuppressWarnings("rawtypes")
	static class orderVal implements Comparator{
		@Override
		public int compare(Object o1, Object o2) {
			PokerFaceFile p1=(PokerFaceFile) o1;
			PokerFaceFile p2=(PokerFaceFile) o2;
			return new Double(p1.getIndexVal()).compareTo(new Double(p2.getIndexVal()));
		}
	}
}
